#include <string>
#include <algorithm>
#include <iostream>

using std::string;
using std::reverse;
using std::cout;
class Solution
{
public:
    string reverseStr(string s, int k)
    {
        if(k == 1) return s;
        int start = 0;
        int end = 0;
        while(1)
        {
            if((s.size() - start) > k)
            {
                end += k;
                reverse(s.begin() + start, s.begin() + end);
                if((s.size() - start) <= 2 * k)
                    break;
                start = end + k;
                end = start;
                continue;
            }

            if((s.size() - start) <= k)
            {
                reverse(s.begin() + start,s.end());
                break;
            }
        }
        return s;
    }

};

int main(int argc, char const *argv[])
{
    Solution s;
    string str = s.reverseStr("abcdefg",3);
    cout << str << "\n";
    return 0;
    // bacdfeg
}
